package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.AppUser;
import com.yunkeji.dao.mapper.AppUserMapper;
import com.yunkeji.dao.vo.AppUserVo;
import com.yunkeji.system.domain.bo.AppUserBo;
import com.yunkeji.system.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户基本信息Service业务层处理
 *
 * @author 老张
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class UserServiceImpl implements IUserService {

    private final AppUserMapper baseMapper;

    /** 查询用户基本信息 */
    @Override
    public AppUserVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询用户基本信息列表 */
    @Override
    public TableDataInfo<AppUserVo> queryPageList(AppUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUser> lqw = buildQueryWrapper(bo);
        Page<AppUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询用户基本信息列表 */
    @Override
    public List<AppUserVo> queryList(AppUserBo bo) {
        LambdaQueryWrapper<AppUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUser> buildQueryWrapper(AppUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlatformId() != null, AppUser::getPlatformId, bo.getPlatformId());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), AppUser::getPhone, bo.getPhone());
        lqw.eq(bo.getCardAuth() != null, AppUser::getCardAuth, bo.getCardAuth());
        lqw.eq(bo.getVideoAuth() != null, AppUser::getVideoAuth, bo.getVideoAuth());
        lqw.eq(bo.getVideoPrice() != null, AppUser::getVideoPrice, bo.getVideoPrice());
        lqw.eq(bo.getVoicePrice() != null, AppUser::getVoicePrice, bo.getVoicePrice());
        lqw.eq(bo.getStatus() != null, AppUser::getStatus, bo.getStatus());
        lqw.eq(bo.getOnlineStatus() != null, AppUser::getOnlineStatus, bo.getOnlineStatus());
        lqw.eq(bo.getPid() != null, AppUser::getPid, bo.getPid());
        return lqw;
    }

    /** 新增用户基本信息 */
    @Override
    public Boolean insertByBo(AppUserBo bo) {
        AppUser add = BeanUtil.toBean(bo, AppUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改用户基本信息 */
    @Override
    public Boolean updateByBo(AppUserBo bo) {
        AppUser update = BeanUtil.toBean(bo, AppUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(AppUser entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除用户基本信息 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
